package org.jeecg.modules.common.service.impl;

import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.common.entity.FluxStatisticalPower;
import org.jeecg.modules.common.service.ICamelliaInFluxStatisticalService;
import org.jeecg.modules.common.service.InFluxDBService;
import org.jeecg.modules.common.entity.FluxStatistical;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname CamelliaInFluxStatisticalServiceImpl
 * @Description TODO
 * @Date 2023/1/5 14:32
 * @Created by 10835
 */
@Slf4j
@Service
public class CamelliaInFluxStatisticalServiceImpl implements ICamelliaInFluxStatisticalService {

    @Autowired
    InFluxDBService inFluxDBService;

    @Override
    public FluxStatistical getFluxStatistical(String fluxString) {
        FluxStatistical fluxStatistical = new FluxStatistical();
        List<FluxTable> fluxTables = inFluxDBService.select(fluxString);
        log.info(fluxTables.toString());
        for (FluxTable fluxTable : fluxTables) {
            if (fluxTable.getRecords() != null) {
                List<FluxRecord> records = fluxTable.getRecords();
                for (FluxRecord record : records) {
                    String result = (String) record.getValues().get("result");
                    if ("mean".equals(result)) {
                        fluxStatistical.setMeanValue((Double) record.getValues().get("_value"));
                    } else if ("max".equals(result)) {
                        fluxStatistical.setMaxValue((Double) record.getValues().get("_value"));
                    }else if ("min".equals(result)){
                        fluxStatistical.setMinValue((Double) record.getValues().get("_value"));
                    } else if ("median".equals(result)) {
                        fluxStatistical.setMedianValue((Double) record.getValues().get("_value"));
                    }
                }
            }
        }
        return fluxStatistical;
    }

    @Override
    public FluxStatisticalPower getFluxStatisticalPower(String fluxString) {
        FluxStatisticalPower fluxStatisticalPower = new FluxStatisticalPower();
        List<FluxTable> fluxTables = inFluxDBService.select(fluxString);
        log.info(fluxTables.toString());
        for (FluxTable fluxTable : fluxTables) {
            if (fluxTable.getRecords() != null) {
                List<FluxRecord> records = fluxTable.getRecords();
                for (FluxRecord record : records) {
                    fluxStatisticalPower.getPowerList().add(new FluxStatisticalPower.Power(record.getTime().atZone(ZoneId.systemDefault()).toLocalDate(), (Double) record.getValue()));
                }
                fluxStatisticalPower.setPowerList(fluxStatisticalPower.getPowerList().stream().distinct().collect(Collectors.toList()));
            }
        }
        return fluxStatisticalPower;
    }
}
